home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / mem.com / MEM.H < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-30  |  6.1 KB  |  213 lines

  1. /*_ mem.h   Fri May 26 1989   Modified by: Walter Bright */
  2. /* $Header: /proj/products/merlin/port/RCS/mem.h,v 1.9 89/06/19 15:15:08 bright Exp $ */
  3. /* Copyright 1986-1988 by Northwest Software    */
  4. /* All Rights Reserved                */
  5. /* Written by Walter Bright            */
  6.  
  7. #ifndef MEM_H
  8. #define MEM_H    1
  9.  
  10. #ifndef TOOLKIT_H
  11. #include    "toolkit.h"
  12. #endif
  13.  
  14. /*
  15.  * Memory management routines.
  16.  *
  17.  * Compiling:
  18.  *
  19.  *    #define MEM_DEBUG 1 when compiling to enable extended debugging
  20.  *    features.
  21.  *
  22.  * Features always enabled:
  23.  *
  24.  *    o mem_init() is called at startup, and mem_term() at
  25.  *      close, which checks to see that the number of alloc's is
  26.  *      the same as the number of free's.
  27.  *    o Behavior on out-of-memory conditions can be controlled
  28.  *      via mem_setexception().
  29.  *
  30.  * Extended debugging features:
  31.  *
  32.  *    o Enabled by #define MEM_DEBUG 1 when compiling.
  33.  *    o Check values are inserted before and after the alloc'ed data
  34.  *      to detect pointer underruns and overruns.
  35.  *    o Free'd pointers are checked against alloc'ed pointers.
  36.  *    o Free'd storage is cleared to smoke out references to free'd data.
  37.  *    o Realloc'd pointers are always changed, and the previous storage
  38.  *      is cleared, to detect erroneous dependencies on the previous
  39.  *      pointer.
  40.  *    o The routine mem_checkptr() is provided to check an alloc'ed
  41.  *      pointer.
  42.  */
  43.  
  44. /********************* GLOBAL VARIABLES *************************/
  45.  
  46. extern int mem_inited;        /* != 0 if mem package is initialized.    */
  47.                 /* Test this if you have other packages    */
  48.                 /* that depend on mem being initialized    */
  49.  
  50. /********************* PUBLIC FUNCTIONS *************************/
  51.  
  52. /***********************************
  53.  * Set behavior when mem runs out of memory.
  54.  * Input:
  55.  *    flag =    MEM_ABORTMSG:    Abort the program with the message
  56.  *                'Fatal error: out of memory' sent
  57.  *                to stdout. This is the default behavior.
  58.  *        MEM_ABORT:    Abort the program with no message.
  59.  *        MEM_RETNULL:    Return NULL back to caller.
  60.  *        MEM_CALLFP:    Call application-specified function.
  61.  *                fp must be supplied.
  62.  *    fp            Optional function pointer. Supplied if
  63.  *                (flag == MEM_CALLFP). This function returns
  64.  *                MEM_XXXXX, indicating what mem should do next.
  65.  *                The function could do things like swap
  66.  *                data out to disk to free up more memory.
  67.  *    fp could also return:
  68.  *        MEM_RETRY:    Try again to allocate the space. Be
  69.  *                careful not to go into an infinite loop.
  70.  */
  71.  
  72. #if __ZTC__
  73. enum MEM_E { MEM_ABORTMSG, MEM_ABORT, MEM_RETNULL, MEM_CALLFP, MEM_RETRY };
  74. void mem_setexception(_2(enum MEM_E,...));
  75. #else
  76. #define MEM_ABORTMSG    0
  77. #define MEM_ABORT    1
  78. #define MEM_RETNULL    2
  79. #define MEM_CALLFP    3
  80. #define MEM_RETRY    4
  81. void mem_setexception(_2(int,...));
  82. #endif
  83.  
  84.  
  85. /****************************
  86.  * Allocate space for string, copy string into it, and
  87.  * return pointer to the new string.
  88.  * This routine doesn't really belong here, but it is used so often
  89.  * that I gave up and put it here.
  90.  * Use:
  91.  *    char *mem_strdup(const char *s);
  92.  * Returns:
  93.  *    pointer to copied string if succussful.
  94.  *    else returns NULL (if MEM_RETNULL)
  95.  */
  96.  
  97. char *mem_strdup(_1(const char *));
  98.  
  99. /**************************
  100.  * Function so we can have a pointer to function mem_free().
  101.  * This is needed since mem_free is sometimes defined as a macro,
  102.  * and then the preprocessor screws up.
  103.  * The pointer to mem_free() is used frequently with the list package.
  104.  * Use:
  105.  *    void mem_freefp(void *p);
  106.  */
  107.  
  108. /***************************
  109.  * Check for errors. This routine does a consistency check on the
  110.  * storage allocator, looking for corrupted data. It should be called
  111.  * when the application has CPU cycles to burn.
  112.  * Use:
  113.  *    void mem_check(void);
  114.  */
  115.  
  116. void mem_check(_0);
  117.  
  118. /***************************
  119.  * Check ptr to see if it is in the range of allocated data.
  120.  * Cause assertion failure if it isn't.
  121.  * Use:
  122.  *    void mem_checkptr(void *ptr);
  123.  */
  124.  
  125. void mem_checkptr(_1(void *));
  126.  
  127. /***************************
  128.  * Allocate and return a pointer to numbytes of storage.
  129.  * Use:
  130.  *    void *mem_malloc(unsigned numbytes);
  131.  *    void *mem_calloc(unsigned numbytes); allocated memory is cleared
  132.  * Input:
  133.  *    numbytes    Number of bytes to allocate
  134.  * Returns:
  135.  *    if (numbytes > 0)
  136.  *        pointer to allocated data, NULL if out of memory
  137.  *    else
  138.  *        return NULL
  139.  */
  140.  
  141. void *mem_malloc(_1(unsigned)),*mem_calloc(_1(unsigned));
  142.  
  143. /*****************************
  144.  * Reallocate memory.
  145.  * Use:
  146.  *    void *mem_realloc(void *ptr,unsigned numbytes);
  147.  */
  148.  
  149. void *mem_realloc(_2(void *,unsigned));
  150.  
  151. /*****************************
  152.  * Free memory allocated by mem_malloc(), mem_calloc() or mem_realloc().
  153.  * Use:
  154.  *    void mem_free(void *ptr);
  155.  */
  156.  
  157. void mem_free(_1(void *));
  158.  
  159. /***************************
  160.  * Initialize memory handler.
  161.  * Use:
  162.  *    void mem_init(void);
  163.  * Output:
  164.  *    mem_inited = 1
  165.  */
  166.  
  167. void mem_init(_0);
  168.  
  169. /***************************
  170.  * Terminate memory handler. Useful for checking for errors.
  171.  * Use:
  172.  *    void mem_term(void);
  173.  * Output:
  174.  *    mem_inited = 0
  175.  */
  176.  
  177. void mem_term(_0);
  178.  
  179. /* The following stuff forms the implementation rather than the
  180.  * definition, so ignore it.
  181.  */
  182.  
  183. #if MEM_DEBUG        /* if creating debug version    */
  184. #define mem_strdup(p)    mem_strdup_debug((p),__FILE__,__LINE__)
  185. #define mem_malloc(u)    mem_malloc_debug((u),__FILE__,__LINE__)
  186. #define mem_calloc(u)    mem_calloc_debug((u),__FILE__,__LINE__)
  187. #define mem_realloc(p,u)    mem_realloc_debug((p),(u),__FILE__,__LINE__)
  188. #define mem_free(p)    mem_free_debug((p),__FILE__,__LINE__)
  189.  
  190. #if PROTOTYPING
  191. extern char *mem_strdup_debug(const char *,char *,int);
  192. extern void *mem_calloc_debug(unsigned,char *,int),
  193.     *mem_malloc_debug(unsigned,char *,int),
  194.     *mem_realloc_debug(void *,unsigned,char *,int);
  195. extern void mem_free_debug(void *,char *,int),mem_freefp(void *);
  196. #else
  197. extern char *mem_strdup_debug();
  198. extern void *mem_calloc_debug(),*mem_malloc_debug(),*mem_realloc_debug();
  199. extern void mem_free_debug(),mem_freefp();
  200. #endif
  201.  
  202. void mem_setnewfileline(_3(void *,char *,int));
  203.  
  204. #else
  205.  
  206. #define mem_freefp    mem_free
  207. #define mem_check()
  208. #define mem_checkptr(p)
  209.  
  210. #endif /* MEM_DEBUG */
  211.  
  212. #endif /* MEM_H */
  213.